import {Component} from '@angular/core';
import {Article} from "../entity/article";
import {Comment} from "../entity/comment";
import {ArticleService} from "../service/article.service";
import {ActivatedRoute} from "@angular/router";
import {ToastyService, ToastOptions, ToastData} from "ng2-toasty";

@Component({
    moduleId: module.id,
    selector: 'my-article',
    templateUrl: 'article.component.html',
    styleUrls: ['article.component.css'],
})

export class ArticleComponent{

  article: Article;
  comment = new Comment('');
  comments: Comment[];

  constructor(private articleService: ArticleService, private route: ActivatedRoute,
              private toastyService:ToastyService) {}

  ngOnInit(): void {
    const id = this.route.snapshot.params['id'];
    this.articleService.getArticle(id).subscribe((article) => {
      this.article = article;
      this.comments = article.comments;
      document.getElementById("content").innerHTML = this.article.content;
    } )
  }

  onSubmit() {
    this.comment.articleId = this.article.id;
    this.articleService.insertComment(this.comment).subscribe(()=> {
      const toastOptions:ToastOptions = {
        title: "提示信息",
        msg: "发布评论成功！",
        showClose: true,
        timeout: 3000,
        theme: 'bootstrap',
        onAdd: (toast:ToastData) => {},
        onRemove: function(toast:ToastData) {}
      };
      this.toastyService.success(toastOptions);
      this.comments.push(new Comment(this.comment.content));
    });
  }

}
